package com.jerry.generator.Impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.jerry.DTO.FileMeta;
import com.jerry.generator.FilenameGenerator;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class DefaultFilenameGenerator implements FilenameGenerator {
    @Override
    public String generate(FileMeta fileMeta, Object... args) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHH");
        String time = now.format(formatter);
        return String.format("f%s%s", time, md5(fileMeta,args[0].toString()));
    }

    private String md5(FileMeta fileMeta, String address) {
        return DigestUtil.md5Hex(
                String.format("%s_%s_%s_%s", address,
                        fileMeta.getFileSize(),
                        fileMeta.getBucketName(),
                        fileMeta.getExtension())
        );
    }

}
